import { createRouter, createWebHashHistory } from 'vue-router'
import cache from '@/utils/cache'

const routes = [
    {
        path:'/loginPage',
        name:'loginPage',
        component:()=>import('@/views/loginPage/index.vue'),
        meta:{title:'登陆'}
    },
    {
        path:'/logonPage',
        name:'logonPage',
        component:()=>import('@/views/logonPage/index.vue'),
        meta:{title:'激活注册'}
    },
    {
        path:'/errorPage',
        name:'errorPage',
        component:()=>import('@/views/errorPage/index.vue'),
        meta:{title:'好像出现问题了！'}
    },
    {
        path:'/mainPage',
        name:'mainPage',
        component:()=>import('@/views/mainPage/index.vue')
    },
    {
        path:'/',
        redirect:'/mainPage',
        component:()=>import('@/views/mainPage/index.vue')
    },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})


//全局前置路由守卫
router.beforeEach((to, from, next) => {
    let os = cache.token.get()
    if(os){
        if(to.path=='/loginPage'){
            next('/')
        }else{
            console.log(to);
            next()
        }
    }else{
        if(to.path=='/loginPage'){
            next()
        }else{
            next('/loginPage')
        }
    }
})

//全局后置路由守卫
router.afterEach((to,from)=>{
	document.title = to.meta.title || 'Web OS'
})

export default router
